import { setUrl, getUrl } from '@/utils/getUrl.js'
import { pinT } from '@/utils/allUrl.js'
// 获取权限 触发三级菜单
export async function getAuthority(that, sessionName) {
  that.activeName = '' // 重置状态
  that.jurisdiction = [] // 重置为空
  // if (that) {
  //   that.$router.push('/excel')
  //   return
  // }
  var url = getUrl(sessionName) // 地址
  var accessName = that.$route.name
  const params = new URLSearchParams()
  params.append('QXFWLJ', accessName)
  const { data: res } = await that.$http.post(`${pinT}quanXian/selectQuanXian`, params)
  const mainLoad = ''
  console.log('getAuthority---', mainLoad)
  that.$emit('controlMainRefresh', mainLoad)
  // that.mainLoad = false
  // console.log(res)
  if (res.code === '0000') {
    var list = res.data
    // 返回没有权限列表 先去除
    if (res.data.length < 1) {
      that.$router.push('/excel')
      return
    }
    var menus = []
    if (list.length < 1) {
      setUrl('tabs', JSON.stringify(list))
      setUrl('jurisdiction', JSON.stringify(menus))
      return
    }
    var newArr = [
      {
        id: list[0].id,
        jurisdiction: list[0].children,
        path: list[0].qxfwlj,
        title: list[0].qxmc
      }
    ]
    // 判断是否存在tabs 不存在赋值接口中的
    if (getUrl('tabs')) {
    } else {
      setUrl('tabs', JSON.stringify(newArr))
    }
    menus = res.data[0].children
    setUrl('jurisdiction', JSON.stringify(menus))
    if (menus.length === 0) {
      return that.$router.push('/excel')
    }
    let obj = {}
    menus.forEach(item => {
      obj = {
        name: item.qxmc,
        path: item.qxfwlj
      }
      that.jurisdiction.push(obj)
    })
    //   判断本地存储中是否有地址
    that.activeName = accessName
    if (url) {
      that.activeName = url
    }
    setUrl(sessionName, accessName) // 存储当前界面url
  }
  //   else {
  //     var m1 = JSON.parse(getUrl('jurisdiction'))
  //     let o1 = {}
  //     m1.forEach(item => {
  //       o1 = {
  //         name: item.qxmc,
  //         path: item.qxfwlj
  //       }
  //       that.jurisdiction.push(o1)
  //     })
  //   }
}
// 获取权限
export async function getTabAuthority(that) {
  clearTimeout(that.allMainTime)
  that.allmMainLoad = 'load'
  console.log('进来了getTabAuthority--', that.allmMainLoad)
  const accessName = that.$route.name
  const params = new URLSearchParams()
  params.append('QXFWLJ', accessName)
  const { data: res } = await that.$http.post(`${pinT}quanXian/selectQuanXian`, params)
  that.allmMainLoad = ''
  console.log('进来了getTabAuthority--成功调用接口', that.allmMainLoad)
  // console.log('----68---', res.data)
  if (res.code === '0000') {
    var list = res.data
    if (list.length < 1) {
      setUrl('tabs', JSON.stringify(list))
      return
    }
    var newArr = [
      {
        id: list[0].id,
        jurisdiction: list[0].children,
        path: list[0].qxfwlj,
        title: list[0].qxmc
      }
    ]
    setUrl('tabs', JSON.stringify(newArr))
    that.tabs = newArr
  }
}
